const URL = "http://wwacoman.com";

export const getPatientList = (api, code) => {
  return fetch(`${URL}/oss/${api}/${code}`).then(resp => {
    let json = resp.json(); // there's always a body
    if (resp.status >= 200 && resp.status < 300) {
      return json;
    } else {
      return json.then(err => {
        throw err;
      });
    }
  });
};

export const toLogin = (username, password, type) => {
  return fetch(`${URL}/oss/login/${username}/${password}/${type}`).then(
    resp => {
      let json = resp.json(); // there's always a body
      if (resp.status >= 200 && resp.status < 300) {
        return json;
      } else {
        return json.then(err => {
          throw err;
        });
      }
    }
  );
};

//for houseman
export const getDoctorList = empCode => {
  return fetch(`${URL}/oss/houseman/${empCode}`).then(resp => {
    let json = resp.json(); // there's always a body
    if (resp.status >= 200 && resp.status < 300) {
      return json;
    } else {
      return json.then(err => {
        throw err;
      });
    }
  });
};

// for individual patient
export const getPatientReports = (api, code) => {
  return fetch(`${URL}/oss/reports/${api}/${code}`).then(resp => {
    let json = resp.json(); // there's always a body
    if (resp.status >= 200 && resp.status < 300) {
      return json;
    } else {
      return json.then(err => {
        throw err;
      });
    }
  });
};

export const getPatientEMR = (api, code, fromDate, toDate) => {
  return fetch(
    `${URL}/oss/vitals/${api}Emr/${code}/${fromDate}/${toDate}`
  ).then(resp => {
    let json = resp.json(); // there's always a body
    if (resp.status >= 200 && resp.status < 300) {
      return json;
    } else {
      return json.then(err => {
        throw err;
      });
    }
  });
};

export const getPatientVital = (api, code) => {
  if (api === "opd") {
    return fetch(`${URL}/oss/vitals/opdVitals/${code}`).then(resp => {
      let json = resp.json(); // there's always a body
      if (resp.status >= 200 && resp.status < 300) {
        return json;
      } else {
        return json.then(err => {
          throw err;
        });
      }
    });
  }
  return fetch(`${URL}/oss/vitals/${api}Vital/${code}`).then(resp => {
    let json = resp.json(); // there's always a body
    if (resp.status >= 200 && resp.status < 300) {
      return json;
    } else {
      return json.then(err => {
        throw err;
      });
    }
  });
};

export const getSearchResult = param => {
  return fetch(`${URL}/oss/search/${param}`).then(resp => {
    let json = resp.json(); // there's always a body
    if (resp.status >= 200 && resp.status < 300) {
      return json;
    } else {
      return json.then(err => {
        throw err;
      });
    }
  });
};

export const getIpdCount = () => {
  return fetch(`${URL}/oss/ipdcount`).then(resp => {
    let json = resp.json(); // there's always a body
    if (resp.status >= 200 && resp.status < 300) {
      return json;
    } else {
      return json.then(err => {
        throw err;
      });
    }
  });
};

export const captureVitals = () => {
  return fetch(`${URL}/oss/vitals/captureVitals`).then(resp => {
    let json = resp.json(); // there's always a body
    if (resp.status >= 200 && resp.status < 300) {
      return json;
    } else {
      return json.then(err => {
        throw err;
      });
    }
  });
};

export const addVitals = ({
  PV_ID,
  REGNO,
  IPD_NO,
  ADD_DATETIME,
  Description,
  V_Value,
  userName,
  VitalDatetime
}) => {
  return fetch(
    `${URL}/oss/vitals/AddVital/${PV_ID}/${REGNO}/${IPD_NO}/${ADD_DATETIME}/${Description}/${V_Value}/${userName}/${VitalDatetime}`
  ).then(resp => {
    let json = resp.json(); // there's always a body
    if (resp.status >= 200 && resp.status < 300) {
      return json;
    } else {
      return json.then(err => {
        throw err;
      });
    }
  });
};

export const getSearchResult = param => {
  return fetch(`${URL}/oss/search/${param}`).then(res => res.json());
};
